package com.taurus.myboard.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taurus.myboard.common.vo.Result;
import com.taurus.myboard.entity.Role;
import com.taurus.myboard.entity.Role;
import com.taurus.myboard.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.repository.query.Param;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kanten
 * @since 2023-06-07
 */
@RestController
@RequestMapping("/myboard/role")
@Slf4j
public class RoleController {
    @Resource
    private IRoleService roleService;

    @GetMapping("/all")
    public Result<List<Role>> getAllRoles(){
        List<Role> list = roleService.list();
        return Result.success(list);
    }

    @GetMapping("/list")
    public Result<Map<String, Object>> getRoleList(@RequestParam(value = "roleName", required = false) String roleName,
                                                   @RequestParam(value = "pageNo") Long pageNo,
                                                   @RequestParam(value = "pageSize") Long pageSize){
        // 条件判断
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(roleName), Role::getRoleName, roleName);
        wrapper.orderByDesc(Role::getRoleId);
        // 分页
        Page<Role> page = new Page(pageNo, pageSize);
        roleService.page(page, wrapper);

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("total", page.getTotal());
        data.put("rows", page.getRecords());
        return Result.success(data);
    }

    @PostMapping("/add")
    public Result<?> addRole(@RequestBody Role role){
        roleService.addRole(role);
        return Result.success("新增角色成功");
    }

    @PutMapping("/update")
    public Result<?> updateRole(@RequestBody Role role){
        roleService.updateRole(role);
        return Result.success("修改角色成功");
    }

    //    @GetMapping("/byId")
    @GetMapping("{id}")
//    public  Result<Role> getRoleById(@RequestParam(value = "id") Integer id){
    public Result<Role> getRoleById(@PathVariable("id") Integer id){
        Role role = roleService.getRoleById(id);
        return Result.success(role);
    }

    @DeleteMapping("{id}")
    public Result<Role> delRoleById(@PathVariable("id") Integer id){
        try{
            // 使用逻辑删除，mybatis支持
            roleService.removeRoleById(id);
            return Result.success("删除角色成功");
        }catch(EmptyResultDataAccessException e){
            return Result.fail("删除角色失败");
        }
    }
}
